package com.yztc.youbadbad.ui.other.fragment;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.yztc.youbadbad.App;
import com.yztc.youbadbad.R;
import com.yztc.youbadbad.adapter.ADAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class GridcycleFragment extends Fragment {

    @BindView(R.id.textView_guangchang)
    TextView textViewGuangchang;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.iv_message_lay)
    ImageView ivMessageLay;
    @BindView(R.id.vp_ad)
    ViewPager vpAd;
    @BindView(R.id.layout_ad_dot)
    LinearLayout layoutAdDot;

    private Context mContext = App.app;

    //广告条图片地址
    private static final String[] AD_URLS = {
            "http://image.baoimg.net/da/201611/2016111713001653640744.jpg",
            "http://img03.baoimg.net/da/201610/2016102613002920817596.jpg",
            "http://http://image.baoimg.net/da/201611/2016111510000698783569.jpg",
            "http://bmob-cdn-5075.b0.upaiyun.com/2016/07/26/4b0009ff40ba7ca18017c45f1520a612.png"
    };

    //广告条View集合
    private List<ImageView> adViews;

    //广告适配器
    private ADAdapter adAdapter;


    //广告切换到下一页的Handler
    private Handler adHandler = new Handler();

    //让当前页面往后走一页
    private Runnable adRunnable = new Runnable() {
        @Override
        public void run() {
            //获得当前画面的位置
            int position = vpAd.getCurrentItem();
            position++;
            //重新设置位置
            vpAd.setCurrentItem(position);

            //无限自动轮播实现了
            adHandler.postDelayed(adRunnable, 2000);
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public GridcycleFragment() {
        // Required empty public constructor

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gridcycle, container, false);
        ButterKnife.bind(this, view);
        return view;


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initViewPager();
        initADDot();
    }

    @Override
    public void onPause() {
        super.onPause();
        adHandler.removeCallbacks(adRunnable);
    }

    @Override
    public void onResume() {
        super.onResume();
        //自动轮播
        adHandler.postDelayed(adRunnable, 2000);
    }

    //初始化广告圆点
    private void initADDot() {
        for (int i = 0; i < AD_URLS.length; i++) {
            //动态生成View
            ImageView iv = new ImageView(mContext);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.leftMargin = 20;
            iv.setLayoutParams(layoutParams);
            if (i == 0) {
                iv.setImageResource(R.drawable.dot);
            } else {
                iv.setImageResource(R.drawable.dot);
            }
            layoutAdDot.addView(iv);
        }

    }

    private void initViewPager() {
        adAdapter = new ADAdapter(adViews);
        vpAd.setAdapter(adAdapter);
        vpAd.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % adViews.size());


        //ViewPager滑动监听
        vpAd.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                //重新设置圆点状态
                setADDot(position % adViews.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        //ViewPager的触屏处理，解决触屏时停止无限轮播
        vpAd.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int actiton = motionEvent.getAction();
                switch (actiton) {
                    case MotionEvent.ACTION_DOWN: //按下
                        adHandler.removeCallbacks(adRunnable);
                        break;
                    case MotionEvent.ACTION_UP: //放开
                        adHandler.postDelayed(adRunnable, 4000);
                        break;
                }
                return false;
            }
        });
    }

    //初始化广告条View集合
    private void initData() {
        adViews = new ArrayList<>();
        for (int i = 0; i < AD_URLS.length; i++) {
            //动态生成View
            ImageView iv = new ImageView(mContext);
            iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            //使用Picasso加载网络图片
            Picasso.with(mContext).load(AD_URLS[i]).into(iv);
            adViews.add(iv);
        }
    }

    //广告圆点高亮显示
    private void setADDot(int position) {
        for (int i = 0; i < layoutAdDot.getChildCount(); i++) {
            ImageView iv = (ImageView) layoutAdDot.getChildAt(i);
            if (i == position) {
                iv.setImageResource(R.drawable.msgtx);
            } else {
                iv.setImageResource(R.drawable.icon_event_indicator_normal);
            }
        }
    }

}
